package com.example.moban.view.dialog;

import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;


import com.example.moban.R;
import com.rzhd.coursepatriarch.common.utils.ScreenUtils;

/**
 * 登录成功
 *
 * @author yql
 * @date: 2018/11/2.
 */
public class SuccessLoginDialog extends DialogFragment {

    private final String TAG = getClass().getSimpleName();

    private OnDialogCancelListener mDialogCancelListener;
    private TextView textView;
    private String content;
    private Drawable backgroundColor;
    private int alpha = 150;
    /**
     * 自动消失时间
     */
    private int dismissTime = 600;

    private int textSize = 0;

    private Handler mHandler = new Handler();

    @Override
    public void onStart() {
        super.onStart();
        //设置背景透明度
        if (alpha == 0) {
            WindowManager.LayoutParams layoutParams = getDialog().getWindow().getAttributes();
            layoutParams.dimAmount = 0.0f;
            getDialog().getWindow().setAttributes(layoutParams);
        } else {
            //设置圆角
            Rect displayRectangle = new Rect();
            Window window = getDialog().getWindow();
            window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
            //此处可以设置dialog显示的位置
            window.setGravity(Gravity.CENTER);
            window.setBackgroundDrawableResource(com.rzhd.coursepatriarch.common.R.drawable.shape_white_radius10);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //去掉默认标题栏  不然会出现界面留白(requestWindowFeature方法必须在setContentView加载布局之前调用。)
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        //对话框背景色 原有边框会自动消失
        if (alpha == 0) {
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }//
        getDialog().setCancelable(false);
        getDialog().setCanceledOnTouchOutside(false);
        getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
//            dismiss();
                    return true;
                } else {
                    //这里注意当不是返回键时需将事件扩散，否则无法处理其他点击事件
                    return false;
                }
            }
        });
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_success_login, container, false);
        textView = view.findViewById(R.id.successText);
        textView.setBackgroundDrawable(backgroundColor);
        textView.setText(content);
        if (textSize != 0) {
            textView.setTextSize(ScreenUtils.sp2px(getActivity(), textSize));
        }
        if (mDialogCancelListener != null) {
            mHandler.postDelayed(mRunnable, dismissTime);
        }
        return view;
    }

    public void showDialog(FragmentManager manager, String content) {
        this.content = content;
        show(manager, TAG);
    }

    public void showDialog(FragmentManager manager, String content, int textSize) {
        this.content = content;
        this.textSize = textSize;
        show(manager, TAG);
    }

    public void showDialog(FragmentManager manager, String content, Drawable backgroundColor, int alpha) {
        this.content = content;
        this.backgroundColor = backgroundColor;
        this.alpha = alpha;
        show(manager, TAG);
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        //暂时解决 IllegalStateException: Can not perform this action after onSaveInstanceState
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        // 这里吧原来的commit()方法换成了commitAllowingStateLoss()
        ft.commitAllowingStateLoss();
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }

    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            if (getActivity() == null || getActivity().isDestroyed() || getActivity().isFinishing()) {
                return;
            }
            dismiss();
            mDialogCancelListener.onDismiss();
        }
    };

    public void setDialogCancelListener(OnDialogCancelListener dialogCancelListener) {
        mDialogCancelListener = dialogCancelListener;
    }

    public interface OnDialogCancelListener {
        void onDismiss();
    }

}
